//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    active: 0,
    isLoad: false,
    lastTapTime: 0,
    swiperList: [{}, {}, {}],
    favorFilmList: [{}, {}],
    newFilmList: [{}, {}],
    prevueFilmList: [{}, {}],
    filmNews: [{}, {}, {}, {}],
    showingMovie: [],
    upcomingMovie: [],
    upcomingMovieBydate: [],
    toast: '',
    url: app.globalData.url,
    currentCity: '',
    addresse: '正在获取定位',
    maoyanComing: [],

  },
  onChange(event) {
    // event.detail 的值为当前选中项的索引
    this.setData({
      active: event.detail
    });
  },
  onClick(e) {
    var curTime = e.timeStamp
    var lastTime = e.currentTarget.dataset.time
    if (curTime - lastTime > 0) {
      if (curTime - lastTime < 300) { //是双击事件
        this.setData({
          loadModal: true
        })
        var myThis = this
        this.getRefreashInfo();
        this.getTab2Info();
      }
    }
    this.setData({
      lastTapTime: curTime
    })
  },
  onLoad: function (options) {
    if (options.id === '1') {
      this.setData({
        active: 1
      })
    }
    this.maoyanInfo();
    this.getLocation();
    this.getMovieInfo();
    this.getTab2Info();
  },
  getLocation: function () {
    var page = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        // success  
        var longitude = res.longitude
        var latitude = res.latitude
        wx.request({
          url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=PBHBZ-BM5CI-STHGA-5FUOM-AQGCT-EBBEU&get_poi=1',
          data: {},
          header: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            // success  
            var city = res.data.result.address_component.city;
            var addresse = res.data.result.formatted_addresses.recommend

            city = city.substring(0, (city.length - 1))
            page.setData({
              currentCity: city,
              addresse: addresse
            });
          },
          fail: function () {
            page.setData({
              currentCity: "fail"
            });
          },
        })
      }
    })
  },
  getMovieInfo: function () {
    var myThis = this
    wx.request({
      url: app.globalData.url + '/wechatapi/getSwiper',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        myThis.setData({
          swiperList: res.data.data
        })
      },
      fail(res) {
        myThis.setData({
          swiperList: res.data,
          isLoad: true
        })
      },
    })
    wx.request({
      url: app.globalData.url + '/wechatapi/getHomeComingFilm',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        myThis.setData({
          favorFilmList: res.data.data
        })
      }
    })
    wx.request({
      url: app.globalData.url + '/wechatapi/getHomeUpcomingFilm',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        myThis.setData({
          newFilmList: res.data.data
        })
      }
    })
    wx.request({
      url: app.globalData.url + '/wechatapi/getMovieInform',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        myThis.setData({
          toast: res.data.data.toast
        })
      }
    })
    wx.request({
      url: app.globalData.url + '/wechatapi/getMovieNews',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        myThis.setData({
          filmNews: res.data.data
        })
      }
    })
  },
  getTab2Info: function () {
    var myThis = this;
    var now = new Date();
    var nowdate = now.toISOString().slice(0, 10).replace(/-/g, '')
    wx.request({
      url: app.globalData.url + '/wechatapi/getShowingFilm?date=' + nowdate,
      success(res) {
        myThis.setData({
          showingMovie: res.data.data
        })
      }
    })
    wx.request({
      url: app.globalData.url + '/wechatapi/getUpcomingFilmDate',
      success(res) {
        myThis.setData({
          upcomingMovieBydate: res.data.data
        })
      }
    })
    wx.request({
      url: app.globalData.url + '/wechatapi/getUpcomingFilmLimit',
      success(res) {
        myThis.setData({
          upcomingMovie: res.data.data
        })
      }
    })
  },
  getRefreashInfo() {
    var myThis = this;
    wx.request({
      url: app.globalData.url + '/wechatapi/getSwiper',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        myThis.setData({
          swiperList: res.data.data
        })
      },
      fail(res) {
        myThis.setData({
          swiperList: res.data,
          isLoad: true
        })
      },
    })
    wx.request({
      url: app.globalData.url + '/wechatapi/getHomeComingFilm',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        myThis.setData({
          favorFilmList: res.data.data
        })
      }
    })
    wx.request({
      url: app.globalData.url + '/wechatapi/getHomeUpcomingFilm',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        myThis.setData({
          newFilmList: res.data.data
        })
      }
    })
    wx.request({
      url: app.globalData.url + '/wechatapi/getMovieInform',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        myThis.setData({
          toast: res.data.data.toast
        })
      }
    })
    wx.request({
      url: app.globalData.url + '/wechatapi/getMovieNews',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        setTimeout(() => {
          myThis.setData({
            loadModal: false
          })
          wx.showToast({
            title: '更新成功',
            icon: 'success',
          })
        }, 800)
        myThis.setData({
          filmNews: res.data.data,
          isLoad: true
        })
      },
      fail(res) {
        setTimeout(() => {
          myThis.setData({
            loadModal: false
          })
          wx.showToast({
            title: '更新失败',
            icon: 'none',
          })
        }, 800)
        myThis.setData({
          isLoad: true
        })
      },
    })
  },
  maoyanInfo() {
    var page = this;
    wx.request({
      url: 'https://wx.maoyan.com/mmdb/movie/v1/list/wish/order/coming.json?ci=20&limit=30&offset=0',
      header: {
        'x-host': 'http://maoyanapi.vip.sankuai.com'
      },
      success(res) {
        page.setData({
          maoyanComing: res.data.data.coming
        })
      }
    })
  },
})